﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

using RL.ObjectLayer;


namespace RL.DataAccessLayer
{
    public class UserDAL
    {
        private DBConn _dbConnect;

        public UserDAL()
        {
            _dbConnect = new DBConn();
        }

        public UserOL GetUser(UserOL userOL)
        {
            SqlConnection connection = null;
            DataSet ds = null;
            UserOL user = null;

            try
            {
                connection = _dbConnect.OpenConnection();
                _dbConnect.BeginTransaction();
                SqlCommand selectCommand = new SqlCommand("GetUser", connection);
                selectCommand.CommandType = CommandType.StoredProcedure;
                selectCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = userOL.UserName;
                selectCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = userOL.Password;
                SqlDataAdapter da = new SqlDataAdapter(selectCommand);
                ds = new DataSet();
                _dbConnect.CommitTransaction();
                da.Fill(ds);
                _dbConnect.CloseConnection();
                user = new UserOL();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    user.UserID = int.Parse(row["UserID"].ToString());
                    user.UserName = row["UserName"].ToString();
                    user.FirstName = row["FirstName"].ToString();
                    user.LastName = row["LastName"].ToString();
                    user.RoleId = int.Parse(row["RoleId"].ToString());
                }
                return user;

            }
            catch (Exception ex)
            {
                string error = ex.Message;
                _dbConnect.RollbackTransaction();
                _dbConnect.CloseConnection();
            }
            finally
            {
                _dbConnect.CloseConnection();
            }
            return user;
        }
    }
}
